package com.ostrich.cloud.ostrichUserService.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ostrich.cloud.common.result.PageResult;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichPermission.client.PermissionClient;
import com.ostrich.cloud.ostrichPermission.client.param.AddUserParam;
import com.ostrich.cloud.ostrichUserService.common.CustomerResultCode;
import com.ostrich.cloud.ostrichUserService.common.RoleLevel;
import com.ostrich.cloud.ostrichUserService.common.exception.CusException;
import com.ostrich.cloud.ostrichUserService.common.stateEnum.CluStateEnum;
import com.ostrich.cloud.ostrichUserService.common.utils.Algorithm;
import com.ostrich.cloud.ostrichUserService.infra.mapper.AdminMapper;
import com.ostrich.cloud.ostrichUserService.infra.mapper.BusinessmanMapper;
import com.ostrich.cloud.ostrichUserService.infra.mapper.UserParentMapper;
import com.ostrich.cloud.ostrichUserService.infra.model.Admin;
import com.ostrich.cloud.ostrichUserService.infra.model.Businessman;
import com.ostrich.cloud.ostrichUserService.infra.model.UserParent;
import com.ostrich.cloud.ostrichUserService.service.AdminService;
import com.ostrich.ostrichUserService.dto.AdminDto;
import com.ostrich.ostrichUserService.dto.bussiness.BusinessmanDto;
import com.ostrich.ostrichUserService.param.admin.RegAdminParam;
import com.ostrich.ostrichUserService.param.bussiness.LoginBusParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-10-26
 */
@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private PermissionClient permissionClient;


    @Resource
    private UserParentMapper userParentMapper;
    @Override
    public void addAdmin(RegAdminParam regAdminParam) {
        AddUserParam userParam = new AddUserParam();
        userParam.setUsername(regAdminParam.getUsername());
        /**
         * 密码进行加密
         */
        userParam.setPassword(Algorithm.md5(regAdminParam.getPassword()));
        userParam.setRoleId(RoleLevel.STORE);
        Result result = permissionClient.addUser(userParam);
        if (result.getCode()!=200){
            throw new CusException(CustomerResultCode.USERNAME_COPY_EXCEPTION);
        }
        Integer parentId = (Integer) result.getData();

        Admin admin = BeanUtil.copyProperties(regAdminParam, Admin.class);

        admin.setAdminStatus(CluStateEnum.NORMAL.getCode());
        admin.setParentId(parentId);

        int businessmanId = adminMapper.insert(admin);
    }

    @Override
    public UserParent adminLogin(LoginBusParam loginBusParam) {
        log.info("加密的密码是{}",Algorithm.md5(loginBusParam.getPassword()));
        QueryWrapper<UserParent> query = new QueryWrapper<>();
        query.eq("username",loginBusParam.getUsername())
                .eq("password",Algorithm.md5(loginBusParam.getPassword()));
        UserParent userParent = userParentMapper.selectOne(query);
        if (userParent==null){
            throw new CusException(CustomerResultCode.PASSWORD_EXCEPTION);
        }
        if(CluStateEnum.CANCEL.getCode().equals(userParent.getStatus())){
            throw new CusException(CustomerResultCode.CANCEL_EXCEPTION);
        }

        return userParent;
    }

    @Override
    public void updateAdmin(Integer parentId, String status) {
        UpdateWrapper<UserParent> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("parent_id",parentId)
                .set("status",status);
        userParentMapper.update(null,updateWrapper);
        UpdateWrapper<Admin> updateCuWrapper = new UpdateWrapper<>();
        updateCuWrapper.eq("parent_id",parentId)
                .set("admin_status",status);
        adminMapper.update(null,updateCuWrapper);
    }

    @Override
    public PageResult<AdminDto> getAllAdmins(Integer current, Integer size, String status) {
        PageResult<AdminDto> pageResult = new PageResult<>();
        Page<AdminDto> page = new Page<>(current,size);

        IPage<AdminDto> allBusiness = adminMapper.getAllAdmins(page, status);

        pageResult.setElements(allBusiness.getRecords());

        log.info("当前页{}",allBusiness.getCurrent());
        pageResult.setPage(allBusiness.getCurrent());

        log.info("每页的大小{}",allBusiness.getSize());
        pageResult.setPageSize(allBusiness.getSize());

        log.info("总数{}",allBusiness.getTotal());
        pageResult.setTotal(allBusiness.getTotal());

        log.info("总的页数{}",allBusiness.getPages());
        pageResult.setTotalPage(allBusiness.getPages());


        return pageResult;

    }
}
